package telas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

public class Servidor {

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private TabFolder tabFolder = null;
	private Composite composite = null;
	private Label nickLabel = null;
	private Text nickText = null;
	private Label portLabel = null;
	private Text portText = null;
	private Composite composite1 = null;
	private Button okButton = null;
	private Button fecharButton = null;
	private String nick = null;
	private int porta;
	private boolean okPressed = false;
	
	public String getNick() {
		return nick;
	}
	public boolean isOkPressed() {
		return okPressed;
	}
	public int getPorta() {
		return porta;
	}
	private void centerScreen(Shell shell)
	{
	    int x = Display.getCurrent().getPrimaryMonitor().getClientArea().x + (Display.getCurrent().getPrimaryMonitor().getClientArea().width - shell.getBounds().width) / 2;
	    int y = Display.getCurrent().getPrimaryMonitor().getClientArea().y + (Display.getCurrent().getPrimaryMonitor().getClientArea().height - shell.getBounds().height) / 2;
	    shell.setLocation(x, y);
	}
	public void open()
	{
		createSShell();
		centerScreen(sShell);
		sShell.setVisible(true);
		nickText.setFocus();
		
		Display display = Display.getCurrent();

		while (!sShell.isDisposed())
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
		sShell.dispose();
	}
	private void createSShell() 
	{
		sShell = new Shell(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		sShell.setText("DamasRede v1.0");
		sShell.setBackground(new Color(Display.getCurrent(), 236, 233, 216));
		createTabFolder();
		sShell.setSize(new Point(298, 225));
		sShell.setLayout(null);
	}
	/**
	 * This method initializes tabFolder	
	 *
	 */
	private void createTabFolder() {
		tabFolder = new TabFolder(sShell, SWT.NONE);
		createComposite();
		tabFolder.setBounds(new Rectangle(7, 7, 277, 178));
		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("Servidor");
		tabItem.setControl(composite);
	}
	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(null);
		nickLabel = new Label(composite, SWT.NONE);
		nickLabel.setBounds(new Rectangle(13, 17, 117, 13));
		nickLabel.setText("Nick");
		nickText = new Text(composite, SWT.BORDER);
		nickText.setBounds(new Rectangle(13, 35, 197, 19));
		portLabel = new Label(composite, SWT.NONE);
		portLabel.setBounds(new Rectangle(13, 61, 88, 14));
		portLabel.setText("Porta");
		portText = new Text(composite, SWT.BORDER);
		portText.setBounds(new Rectangle(13, 79, 76, 19));
		portText.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{
				if(!Character.isDigit(e.keyCode))
				{
					if(e.keyCode == 8)
						return;
					if(e.keyCode == 13)
					{
						criar();
					}
					e.doit = false;
				}
			}
		});
		createComposite1();
	}
	/**
	 * This method initializes composite1	
	 *
	 */
	private void criar()
	{
		if(nickText.getText().compareTo("") == 0)
			return;
		if(portText.getText().compareTo("") == 0)
			return;
		
		nick = nickText.getText();
		porta = Integer.parseInt(portText.getText());
		okPressed = true;
		
		sShell.dispose();
	}
	private void createComposite1() {
		composite1 = new Composite(composite, SWT.BORDER);
		composite1.setLayout(null);
		composite1.setBackground(new Color(Display.getCurrent(), 232, 232, 232));
		composite1.setLocation(new Point(13, 108));
		composite1.setSize(new Point(248, 36));
		okButton = new Button(composite1, SWT.NONE);
		okButton.setLocation(new Point(2, 5));
		okButton.setText("Criar");
		okButton.setSize(new Point(115, 23));
		okButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				criar();
			}
		});
		fecharButton = new Button(composite1, SWT.NONE);
		fecharButton.setText("Fechar");
		fecharButton.setSize(new Point(115, 23));
		fecharButton.setLocation(new Point(125, 5));
		fecharButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						sShell.dispose();
					}
				});
	}

}
